<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :md="8" :sm="8">
            <a-form-item label="工艺流程名">
              <a-input v-model="queryParam.name" />
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="8">
            <a-form-item label="物料代码">
              <a-input v-model="queryParam.materialCode" />
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="8">
            <a-form-item label="物料名称">
              <a-input v-model="queryParam.materialName" />
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="8">
            <a-form-item label="工序内物料代码">
              <a-input v-model="queryParam['detailMaterialCode']" />
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="8">
            <a-form-item label="工序内物料名称">
              <a-input v-model="queryParam['detailMaterialName']" />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button icon="search" type="primary" @click="searchQuery">查询</a-button>
      <a-button icon="reload" type="primary" @click="searchReset">重置</a-button>
      <a-button icon="download" type="primary" @click="handleExportXls('工艺流程')">导出</a-button>
      <a-upload
        :action="importExcelUrl"
        :headers="tokenHeader"
        :multiple="false"
        :showUploadList="false"
        name="file"
        @change="handleImportExcel"
      >
        <a-button icon="import" type="primary">导入</a-button>
      </a-upload>
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
    </div>
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
        <a style="font-weight: 600">{{ selectedRowKeys.length }}</a
        >项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        class="j-table-force-nowrap"
        :scroll="{ x: true }"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange, type: 'radio' }"
        :customRow="clickThenSelect"
        @change="handleTableChange"
      >
        <template slot="isClosed" slot-scope="text">
          <a-tag :color="text === '未关闭' ? 'red' : 'green'">{{ text }}</a-tag>
        </template>
        <template slot="status" slot-scope="text">
          <a-tag :color="text === '未审核' ? 'red' : 'green'">{{ text }}</a-tag>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record, getSteps)">编辑</a>
          <a-divider type="vertical" />
          <a @click="handleDelete(record.id)">删除</a>
        </span>
      </a-table>
    </div>

    <a-tabs defaultactiveKey="1">
      <a-tab-pane tab="工艺流程详情" key="1">
        <HfRoutingDetailList :main-id="selectedMainId" @getSteps="getSteps"></HfRoutingDetailList>
      </a-tab-pane>
    </a-tabs>
    <hf-routing-modal ref="modalForm" @ok="modalFormOk"></hf-routing-modal>
  </a-card>
</template>

<script>
import '@/assets/less/TableExpand.less'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import HfRoutingModal from './modules/HfRoutingModal'
import { getAction } from '@api/manage'
import HfRoutingDetailList from '@views/production/HfRoutingDetailList.vue'
import HfProductmissionDetailList from '@views/production/HfProductmissionDetailList.vue'

export default {
  name: 'HfRoutingList',
  mixins: [JeecgListMixin],
  components: {
    HfProductmissionDetailList,
    HfRoutingDetailList,
    HfRoutingModal
  },
  data() {
    return {
      description: '工艺流程管理页面',
      isorter: {
        column: 'createTime',
        order: 'desc'
      },
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '物料代码',
          align: 'center',
          dataIndex: 'materialCode'
        },
        {
          title: '物料名称',
          align: 'center',
          dataIndex: 'materialName'
        },
        {
          title: '规格型号',
          align: 'center',
          dataIndex: 'sap'
        },
        {
          title: '单位',
          align: 'center',
          dataIndex: 'unit_dictText'
        },
        {
          title: '物料类型',
          align: 'center',
          dataIndex: 'materialType_dictText'
        },
        {
          title: '工序名',
          align: 'center',
          dataIndex: 'name'
        },
        {
          title: '备注',
          align: 'center',
          dataIndex: 'remark'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: '/production/hfRouting/list',
        delete: '/production/hfRouting/delete',
        deleteBatch: '/production/hfRouting/deleteBatch',
        exportXlsUrl: '/production/hfRouting/exportXls',
        importExcelUrl: 'production/hfRouting/importExcel'
      },
      superFieldList: [],
      ipagination: {
        current: 1,
        pageSize: 5,
        pageSizeOptions: ['5', '10', '30'],
        showTotal: (total, range) => {
          return ' 共' + total + '条'
        },
        showQuickJumper: false,
        showSizeChanger: true,
        total: 0
      },
      bomId: '',
      bomObj: {},
      selectedMainId: ''
    }
  },
  created() {},
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },
  methods: {
    getSteps(record) {
      return record
    },
    clickThenSelect(record) {
      return {
        on: {
          click: () => {
            this.onSelectChange(record.id.split(','), [record])
          }
        }
      }
    },
    onClearSelected() {
      this.selectedRowKeys = []
      this.selectionRows = []
      this.selectedMainId = ''
    },
    onSelectChange(selectedRowKeys, selectionRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectionRows = selectionRows
      this.selectedMainId = selectedRowKeys[0]
    },
    loadData(arg) {
      if (!this.url.list) {
        this.$message.error('请设置url.list属性!')
        return
      }
      //加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.current = 1
      }
      this.onClearSelected()
      var params = this.getQueryParams() //查询条件
      this.loading = true
      getAction(this.url.list, params)
        .then(res => {
          if (res.success) {
            this.dataSource = res.result.records || res.result
            this.ipagination.total = res.result.total
          } else {
            this.$message.warning(res.message)
          }
        })
        .finally(() => {
          this.loading = false
        })
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>
